"""
@author: wtf
@file: forms.py
@time: 2024/5/10 18:07
@desc: 定义表单对象
"""
import wtforms
from models import EmailCaptchaModel, UserModel
class RegisterForms(wtforms.Form):
    email = wtforms.StringField(validators=[wtforms.validators.Email(message="邮箱格式不正确!")])  # validators 验证器
    captcha = wtforms.StringField(validators=[wtforms.validators.Length(min=4, max=4, message="用户名验证码不正确!")])
    username = wtforms.StringField(validators=[wtforms.validators.Length(min=3, max=20, message="用户名长度不正确!")])
    password = wtforms.StringField(validators=[wtforms.validators.Length(min=4, max=20, message="密码长度不正确!")])
    password_confirm = wtforms.StringField(validators=[wtforms.validators.EqualTo("password", message="两次密码不一致!")])

    # 自定义验证:
    # 1.邮箱是否存在 2.验证码是否正确
    def validate_email(self, field):
        email = field.data
        user = UserModel.query.filter_by(email=email).first()
        if user:
            raise wtforms.ValidationError(message="该邮箱已经被注册!")

    # 验证邮箱验证码是否正确
    def validate_captcha(self, field):
        captcha = field.data
        email = self.email.data
        captcha_model = EmailCaptchaModel.query.filter_by(email=email, captcha=captcha)
        if not captcha_model:
            raise wtforms.ValidationError(message="邮箱或者验证码错误!")

class LoginForms(wtforms.Form):
    email = wtforms.StringField(validators=[wtforms.validators.Email(message="邮箱格式不正确!")])  # validators 验证器
    password = wtforms.StringField(validators=[wtforms.validators.Length(min=3, max=20, message="密码长度不正确!")])


# 验证发布的信息是否符合规格!
class PublicQuestionForms(wtforms.Form):
    question = wtforms.StringField(validators=[wtforms.validators.Length(min=3, max=100, message="问题格式不正确!")])
    content = wtforms.StringField(validators=[wtforms.validators.Length(min=3, message="内容格式不正确!")])

class PublicAnswerForm(wtforms.Form):
    content = wtforms.StringField(validators=[wtforms.validators.Length(min=1, message="内容格式不正确!")])
    question_id = wtforms.IntegerField(validators=[wtforms.validators.input_required("必须要传入一个值!")])
